package com.china08.yxyapi.api.home.activity.marker;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerCatalogRepository;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 区县教育局-竞赛报名-竞赛项目列表展示
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class YxyMakerCatalogLstController extends YxyApiBaseController {
    @Autowired
    private YxyMakerCatalogRepository yxyMakerCatalogRepository;
    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @RequestMapping(value = "/api_yw/home/activity/competition/catalog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        List<RespModel> respModels = new ArrayList<>();
        // 得到当前登录用户
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();

        List<YxyMakerCatalog> list = new ArrayList<>();
        if ("all".equals(reqModel.pId)) {
            list = yxyMakerCatalogRepository.findByPhaseAndPidNot(reqModel.phase, "0");
        } else {
            list = yxyMakerCatalogRepository.findByPhaseAndPid(reqModel.phase, reqModel.pId);
        }
        for (YxyMakerCatalog cat : list) {
            RespModel respModel = new RespModel();
            respModel.id = cat.getId();
            respModel.name = cat.getName();
            respModel.counts = yxyMakerWorksRepository.countBySchoolIdAndCatalog1AndCatalog2(schoolId, cat.getPid(),
                    cat.getId());
            int pid = Integer.parseInt(cat.getPid());
            if (pid < 5) {
                respModel.pname = "评选类";
            } else if (pid < 8) {
                respModel.pname = "创客竞赛";
            } else if (pid < 11) {
                respModel.pname = "机器人竞赛";
            } else if (pid < 14) {
                respModel.pname = "益智类竞赛";
            } else {
                respModel.pname = "学科类竞赛";
            }
            respModels.add(respModel);
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String phase;// 学段 小学：1 初中：2 高中：3 中职：4
        public String pId;// 创客分类的pId,全部传all

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(pId, null), "创客分类的pId不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "学段不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        // 创客分类id
        public String id;
        // 创客分类的名称
        public String name;
        // 报名数量
        public Integer counts;
        // 创客分类父类名称
        public String pname;
    }
}
